package com.meizu.asum.richtextsdk.tools.selectpictools.tools;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.meizu.asum.richtextsdk.tools.selectpictools.selecter.XPictureSelector;

import java.io.File;
import java.util.Date;

/**
 * 获取图片工具
 *
 * @Author XJW
 * @CreateTime 2017/6/2
 */
public class SelectTools {
    public static void fromGallery(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setType("image/*");
        intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        activity.startActivityForResult(intent, XPictureSelector.REQUEST_CODE_GALLERY_IMAGE);
    }

    public static Uri fromCamera(Activity activity) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        String rootDir = activity.getExternalFilesDir(Environment.DIRECTORY_DCIM).getPath() + "/XMediaImageSelector/CameraCache/";
        File destDir = new File(rootDir);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        String name = rootDir + new Date().getTime() + ".jpg";
        File file = new File(name);
        Uri uri = Uri.fromFile(file);

        intent.putExtra("output", Uri.fromFile(file));// 输出到文件
        activity.startActivityForResult(intent, XPictureSelector.REQUEST_CODE_CAMERA_IMAGE);

        return uri;
    }
}
